package com.homestay.service.impl;

import com.homestay.beans.Books;
import com.homestay.beans.Vip;
import com.homestay.dao.BooksDao;
import com.homestay.dao.VipDao;
import com.homestay.dao.impl.BooksDaoImpl;
import com.homestay.dao.impl.VipDaoImpl;
import com.homestay.exception.UsedException;
import com.homestay.service.VipService;

import java.util.Collections;
import java.util.List;

public class VipServiceImpl implements VipService {
    VipDao vipDao = new VipDaoImpl();
    BooksDao booksDao = new BooksDaoImpl();

    @Override
    public int addVip(Vip vip) throws UsedException {
        int cardCount = vipDao.queryVipByCard(vip.getCard()).size();
        int phoneCount = vipDao.queryVipByPhone(vip.getPhone()).size();

        if(cardCount > 0) {
            throw new UsedException("该身份证号已被使用，请更换后重试");
        }
        if(phoneCount > 0) {
            throw new UsedException("该手机号已被使用，请更换后重试");
        }

        return vipDao.addVip(vip);
    }

    @Override
    public List<Vip> queryVipList(String phone) {
        return vipDao.queryVipList(phone);
    }

    @Override
    public int deleteVip(String id) throws UsedException {
        Books book = booksDao.getBooksByVipId(Integer.parseInt(id));
        if (book != null) {
            throw new UsedException("该会员已预订房屋，无法删除");
        }
        return vipDao.deleteVipById(id);
    }

    @Override
    public Vip queryVipById(String id) {
        return vipDao.queryVipById(id);
    }

    @Override
    public int updateVip(Vip vip) {
        return vipDao.updateVip(vip);
    }


}
